package com.groupfifteen.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.groupfifteen.dao.SysUserDao;
import com.groupfifteen.entity.SysUserEntity;
import com.groupfifteen.vo.SysUserVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity>
        implements SysUserService {


    @Override
    public Integer AddSysUser(SysUserEntity sysUserEntity) {

        return baseMapper.insert(sysUserEntity);
    }
    @Override
    public Integer DeleteSysUser(Integer userId) {
        return baseMapper.deleteById(userId);
    }

    @Override
    public SysUserEntity GetSysUser(Integer userId) {
        return baseMapper.selectById(userId);
    }

    @Override
    public  Integer UpdateSysUser(SysUserEntity sysUserEntity){
        return baseMapper.updateById(sysUserEntity);
    }

    @Override
    public IPage<SysUserEntity> Query(SysUserEntity sysUserEntity, int pageNo, int pageSize) {

        LambdaQueryWrapper<SysUserEntity> query = Wrappers.lambdaQuery();
        query.eq(sysUserEntity.getId() != null, SysUserEntity::getId, sysUserEntity.getId());

        if( pageNo == 0 )
        {
            pageNo = 1;
        }
        if( pageSize == 0 ){
            pageSize = 10;
        }

        IPage<SysUserEntity> page = Page.of(pageNo, pageSize);

        return baseMapper.selectPage(page, query);
    }

    @Override
    public IPage<SysUserEntity> Query(SysUserVo sysUserVo) {
//        LambdaQueryWrapper<SysUserEntity> query = Wrappers.lambdaQuery();
//        query.eq(sysUserVo.getId() != null, SysUserEntity::getId, sysUserVo.getId());
//
//
//        long pageNo = 1 , pageSize = 10;
//        IPage<SysUserEntity> page = Page.of(pageNo, pageSize);
//
//        return null;
        LambdaQueryWrapper<SysUserEntity> query = Wrappers.lambdaQuery();
        query.eq(sysUserVo.getId() != null, SysUserEntity::getId, sysUserVo.getId())
                .eq(sysUserVo.getUserName() != null, SysUserEntity::getUserName, sysUserVo.getUserName())
                .eq(sysUserVo.getPassword() != null, SysUserEntity::getPassword, sysUserVo.getPassword());

        int currentPage = sysUserVo.getCurrentPage();
        int pageSize = sysUserVo.getPageSize();
        IPage<SysUserEntity> page = new Page<>(currentPage, pageSize);

        return baseMapper.selectPage(page, query);
    }




}
